﻿using Microsoft.Practices.Prism.Interactivity;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using mwc = Microsoft.Windows.Controls;

namespace HobSoft.SoccerBet2014.Common.Wpf.Behavior
{
    public static class Selected
    {
        private static readonly DependencyProperty SelectedCommandBehaviorProperty =
            DependencyProperty.RegisterAttached("SelectorCommandBehavior", typeof(SelectorCommandBehavior), typeof(Selected), null);

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(Selected), new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(Selected), new PropertyMetadata(OnSetCommandParameterCallback));

        public static void SetCommand(Selector control, ICommand command)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(Selected.CommandProperty, command);
        }

        public static ICommand GetCommand(Selector control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(Selected.CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(Selector control, object parameter)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            control.SetValue(Selected.CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(Selector control)
        {
            if (control == null)
            {
                throw new ArgumentNullException("control");
            }
            return control.GetValue(Selected.CommandParameterProperty);
        }

        private static SelectorCommandBehavior GetOrCreateBehavior(Selector control)
        {
            SelectorCommandBehavior clickCommandBehavior = control.GetValue(Selected.SelectedCommandBehaviorProperty) as SelectorCommandBehavior;
            if (clickCommandBehavior == null)
            {
                clickCommandBehavior = new SelectorCommandBehavior(control);
                control.SetValue(Selected.SelectedCommandBehaviorProperty, clickCommandBehavior);
            }
            return clickCommandBehavior;
        }


        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as Selector;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.Command = e.NewValue as ICommand;
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var control = dependencyObject as Selector;
            if (control == null) return;
            var behavior = GetOrCreateBehavior(control);
            behavior.CommandParameter = e.NewValue;
        }
    }

    public class SelectorCommandBehavior : CommandBehaviorBase<Selector>
    {
        public SelectorCommandBehavior(Selector selectableObject)
            : base(selectableObject)
        {
            selectableObject.SelectionChanged += OnSelectionChanged;
            if (selectableObject is DataGrid)
            {
                var datagrid = selectableObject as DataGrid;
                datagrid.SelectedCellsChanged += OnSelectedCellsChanged;
            }
            if (selectableObject is mwc.DataGrid)
            {
                var datagrid = selectableObject as mwc.DataGrid;
                datagrid.SelectedCellsChanged += OnWTCSelectedCellsChanged; ;
            }
        }

        void OnWTCSelectedCellsChanged(object sender, mwc.SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells != null && e.AddedCells.Count > 0)
            {
                CommandParameter = e.AddedCells[0].Item;
                ExecuteCommand(CommandParameter);
            }
        }

        void OnSelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells != null && e.AddedCells.Count > 0)
            {
                CommandParameter = e.AddedCells[0].Item;
                ExecuteCommand(CommandParameter);
            }
        }

        void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CommandParameter = TargetObject.SelectedValue;
            ExecuteCommand(CommandParameter);
        }
    }
}
